#include <core/init.h>
#include <core/irq.h>
#include <core/io.h>
#include <autoconf.h>




static int At91RtcIsr(void *pDev)
{
	if (!(readl(VA(AT91SAM926X_PA_RTTC + RTTC_SR)) & 0x2))
		return IRQ_NONE;

	GuIncTick();

	// printf("%s(): rtc interrupt!", __FUNCTION__);
	return IRQ_HANDLED;
}


static __INIT__ int At91RtcInit(void)
{
	int ret;


	DRIVER_INFO("%s().\n", __FUNCTION__);

	writel(1 << 18, VA(AT91SAM926X_PA_RTTC + RTTC_MR));

	writel(0x8000, VA(AT91SAM926X_PA_RTTC + RTTC_MR));

	ret = RegisterIsr(1, At91RtcIsr, NULL);
	if (ret < 0)
	{
		printf("%s(): RegisterIsr() fail!\n", __FUNCTION__);
		return ret;
	}

	writel((1 << 17) | readl(VA(AT91SAM926X_PA_RTTC + RTTC_MR)), VA(AT91SAM926X_PA_RTTC + RTTC_MR));

	return ret;
}

DRIVER_INIT(At91RtcInit);

